package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.pojo.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.Dishservice;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/dish")
@Slf4j
public class DishController {
    @Autowired
    private Dishservice dishservice;

    @ApiOperation("分页查询菜品")
    @GetMapping("/page")
    public Result<PageResult> findByPage(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishservice.findByPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

//    @ApiOperation("")

    @ApiOperation("新增菜品")
    @PostMapping()
    public Result<String> addDish(@RequestBody  DishDTO dishDTO){
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishservice.addDish(dish);
        return Result.success("新增成功");
    }

    @ApiOperation("批量删除菜品")
    @DeleteMapping()
    public Result<String> deleteByIds(@RequestParam List<Long> ids){
        dishservice.deleteByIds(ids);
        return Result.success();
    }

    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> findDishById(@PathVariable Long id){
         DishVO dishVO = dishservice.findDishById(id);
        return Result.success(dishVO);
    }

    @ApiOperation("修改菜品")
    @PutMapping
    public Result<String> update(@RequestBody DishDTO dishDTO){
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishservice.update(dish);
        return Result.success("修改成功");
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> listByCategoryId(Integer categoryId){
        List<Dish> dishList =dishservice.listByCategoryId(categoryId);
        return Result.success(dishList);
    }

}
